package com.mlk.engUser.tonglian;

import com.mlk.engUser.tonglian.util.DemoUtil;
import com.mlk.engUser.tonglian.util.HttpUtil;
import com.mlk.engUser.tonglian.xml.XmlParser;
import com.mlk.engUser.tonglian.xstruct.common.AipgReq;
import com.mlk.engUser.tonglian.xstruct.common.AipgRsp;
import com.mlk.engUser.tonglian.xstruct.common.InfoReq;
import com.mlk.engUser.tonglian.xstruct.common.InfoRsp;
import com.mlk.engUser.tonglian.xstruct.trans.TransExt;
import com.mlk.engUser.tonglian.xstruct.trans.TransRet;

public class Tranx100014 {

	public static void main(String[] args){
		
		InfoReq infoReq = DemoUtil.makeReq("100014");
        System.out.println("req_sn="+infoReq.getREQ_SN());
		TransExt trans = new TransExt();
		// 业务代码
		trans.setBUSINESS_CODE("09900");//必须使用业务人员提供的业务代码，否则返回“未开通业务类型”
        // 设置的就是商户号
		trans.setMERCHANT_ID(DemoConfig.merchantid);
		// 获取时间吗
		trans.setSUBMIT_TIME(DemoUtil.getNow());
		// 账号名
		trans.setACCOUNT_NAME("徐鲁");
		// 账户一行卡号
		trans.setACCOUNT_NO("6228480283039076718");
		// 私人账户
		trans.setACCOUNT_PROP("0");
		// 转账的金额
		trans.setAMOUNT("1");
		// 这个不填写了
		trans.setBANK_CODE("");
		// 写死
		trans.setCURRENCY("CNY");
		//
		trans.setTEL("");
		//
		trans.setCUST_USERID("");
		trans.setSUMMARY("能源宝提现");

		AipgReq req = new AipgReq();
		req.setINFO(infoReq);
		req.addTrx(trans);
		try{
			//step1 对象转xml
			String xml = XmlParser.toXml(req);
			//step2 加签
			String signedXml = DemoUtil.buildSignedXml(xml);
			//step3 发往通联
			String url = DemoConfig.url+"?MERCHANT_ID="+DemoConfig.merchantid+"&REQ_SN="+infoReq.getREQ_SN();
			System.out.println("============================请求报文============================");
			System.out.println(signedXml);
			String respText = HttpUtil.post(signedXml, url);
			System.out.println("============================响应报文============================");
			System.out.println(respText);
			//step4 验签
			if(!DemoUtil.verifyXml(respText)){
				System.out.println("====================================================>验签失败");
				return;
			}
			System.out.println("====================================================>验签成功");
			//step5 xml转对象
			AipgRsp rsp = XmlParser.parseRsp(respText);
			InfoRsp infoRsp = rsp.getINFO();
            System.out.println(infoRsp);
			System.out.println(infoRsp.getRET_CODE());

			System.out.println(infoRsp.getERR_MSG());
			if("0000".equals(infoRsp.getRET_CODE())){
                System.out.println("提交成功....");
				TransRet ret = (TransRet) rsp.trxObj();
				System.out.println(ret.getRET_CODE());
				System.out.println(ret.getERR_MSG());
				System.out.println(ret.getSETTLE_DAY());
				
			}
		}catch(AIPGException e){
			e.printStackTrace();
		}
	}

}